package card.player;

import card.constant.Command;
import card.context.BaseContext;
import card.context.PlatformContext;
import card.enums.CardTypeEnum;
import card.rule.CardCompareRule;
import card.rule.CardTypeRule;
import card.rule.RuleFactory;
import card.utils.CardUtil;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 玩家
 * @author: ly
 * @date: 2020/8/20 17:39
 */
@Setter
@Getter
public class PlayerModel extends AbstractPlayer {
    /**
     * 是否地主
     */
    private boolean diZhu;

    public PlayerModel(String id) {
        super(id);
        cardList = new ArrayList<>();
    }

    @Override
    public void accept(String card) {
        cardList.add(card);
    }

    @Override
    public void accept(List<String> cardList) {
        cardList.addAll(cardList);
    }

    @Override
    public List<String> order(List<String> cardList) {
        return CardUtil.cartRecomShow(cardList);
    }

    @Override
    public void print() {
        System.out.println("玩家：" + getId() + "-" + getMark());
        System.out.println("纸牌：" + order(cardList) + ",牌数：" + getCardNum());
    }

    @Override
    public boolean leave(String cards) {
        try {
            //是否pass
            if (StringUtils.isNotBlank(cards) && cards.equalsIgnoreCase(Command.PASS)) {
                return true;
            }
            //出牌规则校验
            CardTypeEnum cardTypeEnum = RuleFactory.create(CardTypeRule.class).checkCardType(cards, this);
            if (cardTypeEnum == null) {
                System.out.println("错误：！出牌有误，请检查打出的牌！");
                return false;
            }
            PlatformContext context = BaseContext.getContext();
            //是否第一个出牌
            if (StringUtils.isBlank(context.getPreId()) || this.getId().equalsIgnoreCase(context.getPreId())) {
                //出牌成功,设置context
                updateContext(cards, cardTypeEnum, context);
                return true;
            }
            //比较出牌大小
            if (!RuleFactory.create(CardCompareRule.class).compare(context, cardTypeEnum, cards)) {
                System.out.println("错误：！出牌有误，请检查打出的牌！");
                return false;
            }
            //出牌成功,设置context
            updateContext(cards, cardTypeEnum, context);
            return true;
        } catch (Exception e) {
            System.out.println("未识别命令，请重新输入");
            return false;
        }
    }

    @Override
    public String getMark() {
        return isDiZhu() ? "地主" : "农民";
    }

    /**
     * 更新容器
     *
     * @param cards        打出的牌
     * @param cardTypeEnum 牌型
     * @param context      容器
     */
    private void updateContext(String cards, CardTypeEnum cardTypeEnum, PlatformContext context) {
        context.setPreId(this.getId());
        context.setPreMark(this.getMark());
        context.setPreType(cardTypeEnum);
        context.setPreGiveCards(cards);
        //除去自己手中已经打出的牌，可能将手中已有相同的牌全部清除了，比如打出3 手上有两个3，只需要去掉一张3即可
        List<String> leaveList = CardUtil.leaveConvert(cards);
        List<String> cardList = this.getCardList();
        boolean remove = false;
        for (int i = cardList.size()-1; i>=0; i--) {
            for (int j = leaveList.size()-1; j >=0; j--) {
                if (leaveList.get(j).equalsIgnoreCase(cardList.get(i))) {
                    leaveList.remove(j);
                    remove = true;
                    break;
                }
            }
            if (remove) {
                cardList.remove(i);
                remove = false;
            }

        }
    }


    @Override
    public String toString() {
        return "玩家：" + getId() + ",纸牌：" + cardList + ",牌数：" + cardNum + ",是否地主：" + diZhu;
    }
}
